package robotAi

import MAX_UPDATE_TIMES
import simpleProtobuf.SimpleProtobuf
import xhhx.network.C2sMsg
import xhhx.network.S2cMsg

class LoginAction() : BaseLeafAction<Robot>() {
    private var waitForServer = false
    private var updateTime = 0
    override fun GetDescription(): String {
        return "LoginAction"
    }

    override fun Run(robot: Robot): ActionResult {
        if (updateTime > MAX_UPDATE_TIMES) {
            return FAILED
        }

        if (waitForServer) {
            updateTime++
            return RUNNING
        }
        val login = SimpleProtobuf.Login.newBuilder()
                .setLoginId(robot.id)
                .setPassWord(123456)
                .build()
        val msg = S2cMsg(1, login)
        robot.ch.writeAndFlush(msg)
        waitForServer = true
        return RUNNING
    }

    override fun ReceiveMessage(msg: C2sMsg): ActionResult {
        System.out.println(msg.receive)
        return SUCCESS
    }

    override fun Reset(): RobotAction<Robot> {
        waitForServer = false
        return this
    }
}